package com.study.d015propertybind.fx;



import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.IntegerExpression;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
*@description 单双向绑定的例子
*@author xtayhame
*date 2020年3月5日
*version
*since v1.0
*/
public class BindMain extends Application{
	public static void main(String[] args) {
		//单向绑定
		SimpleIntegerProperty x = new SimpleIntegerProperty(3);
		SimpleIntegerProperty y = new SimpleIntegerProperty(5);
		SimpleIntegerProperty z = new SimpleIntegerProperty(6);
		//x绑定y, x值随y的值的改变而改变,并初始化为y的值
		x.bind(y);
		y.bind(z);
		//绑定具有传递性
		System.out.println("x="+x.get());   //6
		System.out.println("y="+y.get());   //6
		System.out.println("z="+z.get());   //6

		//解绑
		x.unbind();
		x.set(10);
		System.out.println("x="+x.get());   //10
		System.out.println("y="+y.get());   //6
		System.out.println("z="+z.get());   //6

		System.out.println("---------------------------------------------------");

		//双向绑定 , 也具有传递性
		SimpleIntegerProperty x1 = new SimpleIntegerProperty(3);
		SimpleIntegerProperty y1 = new SimpleIntegerProperty(5);
		SimpleIntegerProperty z1 = new SimpleIntegerProperty(6);
		x1.bindBidirectional(y1);
		y1.bindBidirectional(z1);
		System.out.println("x1="+x1.get());   //6
		System.out.println("y1="+y1.get());   //6
		System.out.println("z1="+z1.get());   //6

		x1.set(20);
		y1.set(21);
		z1.set(22);

		System.out.println("x1="+x1.get());   //22
		System.out.println("y1="+y1.get());   //22
		System.out.println("z1="+z1.get());   //22

		y1.set(31);
		z1.set(32);
		x1.set(30);
		System.out.println("x1="+x1.get());   //30
		System.out.println("y1="+y1.get());   //30
		System.out.println("z1="+z1.get());   //30

		x1.unbindBidirectional(y1);
		y1.unbindBidirectional(z1);
		y1.set(41);
		z1.set(42);
		x1.set(40);
		System.out.println("x1="+x1.get());   //41
		System.out.println("y1="+y1.get());   //42
		System.out.println("z1="+z1.get());   //43


		//绑定判断
		System.out.println("***********************值比较判断***************************");
		SimpleIntegerProperty s1 = new SimpleIntegerProperty(3);
		SimpleIntegerProperty s2 = new SimpleIntegerProperty(5);
		System.out.println("s1的值是否大于s2的值："+s1.greaterThan(s2).get());
		System.out.println("s1的值是否大于等于s2的值："+s1.greaterThanOrEqualTo(s2).get());
		System.out.println("s1的值是否小于s2的值："+s1.lessThan(s2).get());
		System.out.println("s1的值是否小于等于s2的值："+s1.lessThanOrEqualTo(s2).get());
		System.out.println("s1取反："+s1.negate().get());
		System.out.println("s1的值是否和s2的值相等[误差在2以内]："+s1.isEqualTo(s2, 2).get());



		System.out.println("***********************值比较判断***************************");


		System.out.println("IntegerExpression类的使用：");
		SimpleIntegerProperty sp1= new SimpleIntegerProperty(5);

		IntegerExpression ie= SimpleIntegerProperty.integerExpression(sp1);

		System.out.println(""+ie.add(5).get());


		launch(args);

	}



	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		Button b1 = new Button("随着窗口的宽度的变化而变化");
		anchorPane.getChildren().add(b1);

		TextField t1 = new TextField();
		TextField t2 = new TextField();

		Button moveBtn = new Button("移动中");
		Slider slider = new Slider(0, 400, 0);
		slider.setPrefWidth(400);

		anchorPane.getChildren().addAll(t1,t2,slider,moveBtn);

		AnchorPane.setTopAnchor(t1, 100.0);
		AnchorPane.setTopAnchor(t2, 100.0);
		AnchorPane.setLeftAnchor(t2, 200.0);

		AnchorPane.setTopAnchor(moveBtn, 200.0);
		AnchorPane.setTopAnchor(slider, 300.0);

		Scene scene = new Scene(anchorPane, 400, 500);
		stage.setScene(scene);
		stage.setTitle("单/双向绑定例子");
		stage.show();

		//例子1：让button的宽度随着窗口的宽度的变化而变化        旧的写法（通过监听）
		b1.setPrefWidth(anchorPane.getWidth());
		/*scene.widthProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				b1.setPrefWidth(newValue.doubleValue());
			}
		} );*/

		//例子1：让button的宽度随着窗口的宽度的变化而变化        新的写法（通过绑定）
		//注意：这里如果使用b1.widthProperty(),则返回的ReadOnlyDobuleProperty，该类没有bind()方法
		b1.prefWidthProperty().bind(scene.widthProperty());



		//*************************例子2：两个个输入框双向绑定********************
		//旧的写法，（通过监听）
		/*t1.textProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				t2.setText(newValue);
			}
		});

		t2.textProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				t1.setText(newValue);
			}
		});*/

		//新的写法（通过绑定）
		//t1.textProperty().bindBidirectional(t2.textProperty());
		//带转换参数的绑定
		t1.textProperty().bindBidirectional(t2.textProperty(), new StringConverter<String>() {

			@Override
			public String toString(String string) {
				if(string !=null && !"".equals(string)){
					return string.replace("5", "伍");
				}
				System.out.println("222:"+string);
				return string;
			}

			@Override
			public String fromString(String string) {
				if(string !=null && !"".equals(string)){
					return string.replace("5", "伍");
				}
				System.out.println("222:"+string);
				return string;
			}
		});

		//例子三： 移动滑块，是按钮跟着移动
		//旧的写法
		/*slider.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				moveBtn.setTranslateX(newValue.doubleValue());
			}
		});*/

		//新的写法
		moveBtn.translateXProperty().bind(slider.valueProperty());



		//Platform.exit();
	}
}
